package com.tledu.meeting_info.service.impl;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.meeting_info.domain.MeetingInfo;
import com.tledu.meeting_info.domain.MeetingRoom;
import com.tledu.meeting_info.domain.MeetingUser;
import com.tledu.meeting_info.domain.dto.MeetingUserDto;
import com.tledu.meeting_info.exception.MeetException;
import com.tledu.meeting_info.mapper.MeetingInfoMapper;
import com.tledu.meeting_info.mapper.MeetingRoomMapper;
import com.tledu.meeting_info.mapper.MeetingUserMapper;
import com.tledu.meeting_info.service.IMeetingInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 会议列表Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@Service
public class MeetingInfoServiceImpl implements IMeetingInfoService {
    @Autowired
    private MeetingInfoMapper meetingInfoMapper;

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;
    @Autowired
    private MeetingUserMapper meetingUserMapper;

    /**
     * 查询会议列表
     *
     * @param id 会议列表主键
     * @return 会议列表
     */
    // 查看按钮
    @Override
    public MeetingUserDto selectMeetingInfoById(Long id) {
        // 先查询会议列表的内容
        MeetingUserDto meetingUserDto = meetingInfoMapper.selectMeetingInfoById(id);

        // 再查询分享人,把分享人放到dto中
        List<Integer> presentList = meetingInfoMapper.selectPresentById(id);
        meetingUserDto.setPresentId(presentList);
        // 再查询与会人,
        List<Integer> sharerList = meetingInfoMapper.selectSharerById(id);
        meetingUserDto.setSharerId(sharerList);
        System.out.println(meetingUserDto);
        return meetingUserDto;
    }

    /**
     * 查询会议列表列表
     *
     * @param meetingInfo 会议列表
     * @return 会议列表
     */
    @Override
    public List<MeetingInfo> selectMeetingInfoList(MeetingInfo meetingInfo) {
        return meetingInfoMapper.selectMeetingInfoList(meetingInfo);
    }

    /**
     * 新增会议列表
     *
     * @param
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertMeetingInfo(MeetingUserDto meetingUserDto) {
        // 在会议列表里新增
        meetingUserDto.setCreateUser(SecurityUtils.getUserId());
        meetingUserDto.setCreateTime(DateUtils.getNowDate());
        int row = meetingInfoMapper.insertMeetingInfo(meetingUserDto);
        // 在中间表里新增分享人和与会人
        Integer meetingId = meetingUserDto.getId();
        List<Integer> shareIds = meetingUserDto.getSharerId();
        for (Integer shareId : shareIds) {
            MeetingUser meetingUser = new MeetingUser(null, meetingId, shareId, 1);
            meetingUserMapper.insertMeetingUser(meetingUser);
        }
        // 在中间表里新增与会人
        List<Integer> presentIds = meetingUserDto.getPresentId();
        for (Integer presentId : presentIds) {
            MeetingUser meetingUser = new MeetingUser(null, meetingId, presentId, 2);
            meetingUserMapper.insertMeetingUser(meetingUser);
        }
        return row;

    }

    /**
     * 修改会议列表
     *
     * @param meetingInfo 会议列表
     * @return 结果
     */
    @Override
    public int updateMeetingInfo(MeetingInfo meetingInfo) {
        return meetingInfoMapper.updateMeetingInfo(meetingInfo);
    }

    /**
     * 批量删除会议列表
     *
     * @param ids 需要删除的会议列表主键
     * @return 结果
     */
    @Override
    public int updateMeetingInfoByIds(Long[] ids) {
        return meetingInfoMapper.updateMeetingInfoByIds(ids);
    }

    /**
     * 删除会议列表信息
     *
     * @param id 会议列表主键
     * @return 结果
     */
//    @Override
//    public int deleteMeetingInfoById(Long id) {
//        return meetingInfoMapper.deleteMeetingInfoById(id);
//    }

    // 新增会议纪要
    @Override
    public int updateSummary(MeetingInfo meetingInfo, Long id) {
        MeetingUserDto meetingUserDto = meetingInfoMapper.selectMeetingInfoById(id);
        long status = meetingUserDto.getStatus();
        if (status == 2) {
            meetingUserDto.setSummary(meetingInfo.getSummary());
            return meetingInfoMapper.updateMeetingSummary(meetingUserDto, id);
        } else {
            throw new MeetException("非法新增会议纪要");
        }
    }

    //取消按钮
    @Override
    public int updateCancel(Long id) {
        //获取那个会议对象
        MeetingInfo meetingInfo = meetingInfoMapper.selectMeetingInfoById(id);
        Long status = meetingInfo.getStatus();
        if (status == 0) {
            meetingInfo.setStatus(3L);
            return meetingInfoMapper.updateMeetingInfo(meetingInfo);
        } else {
            throw new MeetException("无法取消");
        }
    }

    //开始按钮
    @Override
    public int updateStart(Long id) {
        //获取会议对象
        MeetingInfo meetingInfo = meetingInfoMapper.selectMeetingInfoById(id);
        Integer roomId = meetingInfo.getRoomId();
        //获取会议室对象判断会议室是否正在使用
        MeetingRoom meetingRoom = meetingRoomMapper.selectRoomById(roomId);
        System.out.println(meetingRoom);
        Long status = meetingRoom.getStatus();
        if (status == 1) {
            throw new MeetException("会议室正在使用中");
        } else {
            Long status1 = meetingInfo.getStatus();
            if (status1 == 0) {
                meetingInfo.setStatus(1L);
                return meetingInfoMapper.updateMeetingInfo(meetingInfo);
            } else {
                throw new MeetException("无法开始会议");
            }
        }
    }

    //结束按钮
    @Override
    public int updateEnd(Long id) {
        //获取会议对象
        MeetingUserDto meetingUserDto = meetingInfoMapper.selectMeetingInfoById(id);
        //判断是否是已开始状态
        long status = meetingUserDto.getStatus();
        if (status == 1) {
            meetingUserDto.setStatus(2L);
            int row = meetingInfoMapper.updateMeetingInfo(meetingUserDto);
            return row;
        } else {
            throw new MeetException("无法结束会议");
        }
    }


//    @Override
//    public int addMinutes(MeetingInfo meetingInfo) {
//        Long status = meetingInfo.getStatus();
//        if (status == 0) {
//            return meetingInfoMapper.insertMinutes(meetingInfo);
//        }
//        return 0;
//    }
}
